<?php

namespace addons\kzvote\controller;

use addons\kzvote\model\BannerModel;
use addons\kzvote\model\NavModel;
use addons\kzvote\model\TabbarModel;
use app\common\model\kzvote\Notice;
use think\Config;

/**
 * 公共接口
 */
class Common extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 读取配置
     */
    public function getAppid()
    {
        $wxapp = kzvote_getConfig('wxapp');
        if (!$wxapp) {
            $this->error('系统未初始化');
        }

        $this->success('请求成功', $wxapp['app_id']);
    }

    /**
     * 读取配置
     */
    public function config()
    {
        $data['active'] = kzvote_getConfig('active');
        $data['system'] = kzvote_getConfig('system');
        $data['system']['domain'] = $this->request->domain();

        $data['subscription'] = kzvote_getConfig('subscription');
        $color = kzvote_getConfig('color');
        $data['color'] = $color ? $color['color'] : 'blue';
        $data['color_value'] = $color ? $color['value'] : '#00b5ff';

        if (!$data['active'] || !$data['system'] || !$data['color']) {
            $this->error('系统未初始化');
        }

        if ($this->platform == 'wxapp'){
            $data['ad'] = kzvote_getConfig('ad');
            $data['subscription'] = kzvote_getConfig('subscription');
        }elseif ($this->platform == 'wechat'){
            $data['subscription'] = kzvote_getConfig('wechat_subscription');
            $wechat = kzvote_getConfig('wechat');
            if (isset($wechat['wechat_url1']) && $wechat['wechat_url1']){
                $data['wechat_url'] = $wechat['wechat_url'];
                $data['wechat_url1'] = $wechat['wechat_url1'];
            }
        }

        $upload = Config::get('upload');
        $data['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);

        $this->success('请求成功', $data);
    }

    /**
     * 首页数据
     */
    public function index()
    {

        $data['banner'] = BannerModel::field('image,link')->where('status','10')->order('weigh desc')->select();

        $nav = NavModel::where('status','10')->order('weigh desc')->select();
        $navs = $navs[0] = $navs[1] = [];
        if (count($nav) > 8){
            $navs[0] = $navs[1] = [];
            foreach ($nav as $key=>$row){
                if ($key % 2 == 0){
                    array_push($navs[0],$row);
                }else{
                    array_push($navs[1],$row);
                }
            }
        }else{
            $navs[0] = $nav;
        }
        $data['nav'] = $navs;

        $data['notice'] = Notice::where('status', '1')->order('weigh desc')->limit(4)->column('name');

        $this->success('请求成功', $data);
    }

    public function tabbar(){
        $list = (new TabbarModel())->where('status', '10')->order('weigh asc')->select();
        $this->success('请求成功', $list);
    }

    public function getrunenv(){
        $config = [
            'applet_appid'=> config("site.applet_appid"),
            'applet_vote_path'=> config("site.applet_vote_path"),
            'applet_vote_person'=> config("site.applet_vote_person"),
            'envVersion' => config("app_debug") ? 'trial' : 'release',
        ];
        $this->success('请求成功', $config);
    }

}
